package com.mobialia.wikiplaces;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.ads.AdView;
import com.google.android.maps.GeoPoint;


public class WikiplacesDetailActivity extends Activity {

	public static final String TAG = "WikiplacesDetailActivity";
		
	WikiplacesData wikiplacesData;
	int selectedPlace = -1;
	boolean calledFromMap = false; 
	
	public void onCreate(Bundle bundle) {
	    super.onCreate(bundle);

		// No Titlebar
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_PROGRESS);

	    setContentView(R.layout.wikiplaces_detail);

	    // Look up the AdView as a resource and load a request.
		MyAdManager.initialize(this);
	    MyAdManager.loadAd((AdView)this.findViewById(R.id.adView));
	    
        Bundle extras = getIntent().getExtras();
        selectedPlace = extras.getInt("selectedPlace");
        calledFromMap = extras.getBoolean("calledFromMap");
        
        wikiplacesData = WikiplacesData.getInstance(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		Boolean fullscreen = sharedPreferences.getBoolean("fullscreen", false);

		// No Statusbar
		if (fullscreen) {
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		} else {
			getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
		
		wikiplacesData.setSelectedPointIndex(selectedPlace);
        Wikiplace wikiplace = wikiplacesData.getWikiplace(selectedPlace);
        if (wikiplace == null) {
        	finish();
        	return;
        }
		
		TextView distance = (TextView) findViewById(R.id.Distance);
		NumberFormat formatter = new DecimalFormat("0.00");
		if ("km".equals(sharedPreferences.getString("units", "km"))) {
			distance.setText(formatter.format(wikiplace.getDistance()) + " Km");			
		} else {
			distance.setText(formatter.format(wikiplace.getDistance() * 0.621371192) + " Mi");
		}
		
		TextView title = ((TextView) findViewById(R.id.Title));
		title.setText(wikiplace.getTitle());
		
		TextView elevation = ((TextView) findViewById(R.id.Elevation));
		elevation.setText(wikiplace.getElevation() + " m");
		
		TextView summary = ((TextView) findViewById(R.id.Summary));
		summary.setText(wikiplace.getSummary());
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	public void onMapAction(View v)  {
		if (calledFromMap) {
			finish();
		} else {
			Intent intent = new Intent(getApplicationContext(), WikiplacesMapActivity.class);
			startActivity(intent);
			finish();
		}
	}

	public void onWebAction(View v)  {
		String uri = "http://" + wikiplacesData.getWikiplace(selectedPlace).getWikipediaUrl();
		Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
		startActivity(myIntent);
	}
	
	public void onNavigateAction(View v)  {
		GeoPoint point = wikiplacesData.getWikiplace(selectedPlace).getGeoPoint();

		String uri = "google.navigation:q="+Double.valueOf(point.getLatitudeE6())/1000000+","+Double.valueOf(point.getLongitudeE6())/1000000;
		Log.i(TAG, "URI = " + uri + "  " + point.getLatitudeE6() +" "+ point.getLongitudeE6());
				
		Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
		startActivity(myIntent);
	}
	
	public void onBackAction(View v) {
		finish();
	}
	
	public void onPreferencesAction(View v) {
		Intent intent = new Intent(getApplicationContext(), PreferencesActivity.class);
		startActivity(intent);
	}
}